#include "include.h"

/*
 * MACROS (宏定义)
 */

/*
 * CONSTANTS (常量定义)

 */








// Simple GATT Profile Service UUID: 0xFFF0
const uint8_t network_svc_uuid[UUID_SIZE_16] = NETWORK_SVC_UUID;

// static const uint8_t ota_svc_uuid[UUID_SIZE_16] = OTA_SVC_UUID;

/******************************* Characteristic 1 defination *******************************/
// Characteristic 1 UUID: 0xFFF1
// Characteristic 1 data
#define NETWORK_CHAR1_VALUE_LEN 20
uint8_t NETWORK_char1_value[NETWORK_CHAR1_VALUE_LEN] = {0};
// Characteristic 1 User Description
#define NETWORK_CHAR1_DESC_LEN 4
const uint8_t NETWORK_char1_desc[NETWORK_CHAR1_DESC_LEN] = "NTF";

/******************************* Characteristic 2 defination *******************************/
// Characteristic 2 UUID: 0xFFF2
// Characteristic 2 data
#define NETWORK_CHAR2_VALUE_LEN 20
uint8_t NETWORK_char2_value[NETWORK_CHAR2_VALUE_LEN] = {0};
// Characteristic 2 User Description
#define NETWORK_CHAR2_DESC_LEN 6
const uint8_t NETWORK_char2_desc[NETWORK_CHAR2_DESC_LEN] = "WRITE";

/*
 * TYPEDEFS (类型定义)
 */

/*
 * GLOBAL VARIABLES (全局变量)
 */
uint8_t network_svc_id = 0;
// uint8_t ntf_char1_enable = 0;

/*
 * LOCAL VARIABLES (本地变量)
 */
static gatt_service_t network_profile_svc;
static bool network_link_ntf_enable = false;

/*********************************************************************
 * Profile Attributes - Table
 * 每一项都是一个attribute的定义。
 * 第一个attribute为Service 的的定义。
 * 每一个特征值(characteristic)的定义，都至少包含三个attribute的定义；
 * 1. 特征值声明(Characteristic Declaration)
 * 2. 特征值的值(Characteristic value)
 * 3. 特征值描述符(Characteristic description)
 * 如果有notification 或者indication 的功能，则会包含四个attribute的定义，除了前面定义的三个，还会有一个特征值客户端配置(client characteristic configuration)。
 *
 */

const gatt_attribute_t network_profile_att_table[NETWORK_IDX_NB] =
    {

        // Simple gatt Service Declaration
        [NETWORK_IDX_SERVICE] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_PRIMARY_SERVICE_UUID)}, /* UUID */
            GATT_PROP_READ,                                       /* Permissions */
            UUID_SIZE_16,
            /* Max size of the value */ /* Service UUID size in service declaration */
            (uint8_t *)network_svc_uuid,
            /* Value of the attribute */ /* Service UUID value in service declaration */
        },

        // Characteristic 1 Declaration
        [NETWORK_IDX_CHAR1_DECLARATION] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID)}, /* UUID */
            GATT_PROP_READ,                                 /* Permissions */
            0,                                              /* Max size of the value */
            NULL,                                           /* Value of the attribute */
        },
        // Characteristic 1 Value
        [NETWORK_IDX_CHAR1_VALUE] = {
            {UUID_SIZE_16, NETWORK_read_uuid}, /* UUID */
            GATT_PROP_READ | GATT_PROP_NOTI,   /* Permissions */
            NETWORK_CHAR1_VALUE_LEN,           /* Max size of the value */
            NULL,
            /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */
        },

        // Characteristic 4 client characteristic configuration
        [NETWORK_IDX_CHAR1_CFG] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_CLIENT_CHAR_CFG_UUID)}, /* UUID */
            GATT_PROP_READ | GATT_PROP_WRITE,                     /* Permissions */
            2,                                                    /* Max size of the value */
            NULL,
            /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */
        },

        // Characteristic 1 User Description
        [NETWORK_IDX_CHAR1_USER_DESCRIPTION] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID)}, /* UUID */
            GATT_PROP_READ,                                      /* Permissions */
            NETWORK_CHAR1_DESC_LEN,                              /* Max size of the value */
            (uint8_t *)NETWORK_char1_desc,                       /* Value of the attribute */
        },

        // Characteristic 2 Declaration
        [NETWORK_IDX_CHAR2_DECLARATION] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID)}, /* UUID */
            GATT_PROP_READ,                                 /* Permissions */
            0,                                              /* Max size of the value */
            NULL,                                           /* Value of the attribute */
        },
        // Characteristic 2 Value
        [NETWORK_IDX_CHAR2_VALUE] = {
            {UUID_SIZE_16, NETWORK_write_uuid}, /* UUID */
            GATT_PROP_READ | GATT_PROP_WRITE,  /* Permissions */
            NETWORK_CHAR2_VALUE_LEN,           /* Max size of the value */
            NULL,
            /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */
        },
        // Characteristic 2 User Description
        [NETWORK_IDX_CHAR2_USER_DESCRIPTION] = {
            {UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID)}, /* UUID */
            GATT_PROP_READ,                                      /* Permissions */
            NETWORK_CHAR2_DESC_LEN,                              /* Max size of the value */
            (uint8_t *)NETWORK_char2_desc,                       /* Value of the attribute */
        },

};



static uint16_t network_gatt_msg_handler(gatt_msg_t *p_msg)
{
    // co_printf("");
    data(NETWORK_IDX_CHAR1_VALUE,p_msg->conn_idx,network_svc_id);
    co_printf("GATT CCC1:%x\r\n", p_msg->msg_evt);
    switch (p_msg->msg_evt)
    {
    case GATTC_MSG_READ_REQ:
    {


    }
  break;
    case GATTC_MSG_WRITE_REQ:
		{
			
			            if(p_msg->att_idx == NETWORK_IDX_CHAR1_CFG)
            {
                co_printf("ntf_enable:");
                if(*(uint16_t *)p_msg->param.msg.p_msg_data == 0x1)
                {
                    co_printf("true\r\n");
//                    ota_link_ntf_enable = true;
                }
                else
                {
                    co_printf("false\r\n");
//                    ota_link_ntf_enable = false;
                }
            }
					if (p_msg->att_idx == NETWORK_IDX_CHAR2_VALUE)
					{
//						co_printf("char1:");
//						show_data("%s",p_msg->param.msg.p_msg_data);
						BLE_To_UART(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len);//需要这个显示出来，因为co_printf后面会出现乱码。
//						
//						if(strstr((const char *)p_msg->param.msg.p_msg_data, "AT+MAC=") != NULL)
//							
//						co_printf("yeeeee");
					}



    }
    break;
		
		



    case GATTC_MSG_LINK_CREATE:
						co_printf("link_created\r\n");
		network_link_ntf_enable = 1;
            break;
		case GATTC_MSG_LINK_LOST://连接断开，多连接的时候，也触发这个函数
						co_printf("link_lost\r\n");
 					network_link_ntf_enable = 0;
            break;    


    default:
        break;
    }


    return 0;
}


/*********************************************************************
 * @fn      network_gatt_report_notify
 *
 * @brief   Send ota protocol response data.
 *
 *
 * @param   rpt_info_id - report idx of the hid_rpt_info array.
 *          len         - length of the HID information data.
 *          p_data      - data of the HID information to be sent.
 *
 * @return  none.
 */
void network_gatt_report_notify(uint8_t conidx, uint8_t *p_data, uint16_t len)
{
    if (network_link_ntf_enable)
    {
        gatt_ntf_t ntf;
        ntf.conidx = conidx;
        ntf.svc_id = network_svc_id;
        ntf.att_idx = NETWORK_IDX_CHAR1_VALUE;
        ntf.data_len = len;
        ntf.p_data = p_data;
        gatt_notification(ntf);
    }
}


/*********************************************************************
 * @fn      network_gatt_add_service
 *
 * @brief   Simple Profile add GATT service function.
 *			添加GATT service到ATT的数据库里面。
 *
 * @param   None.
 *
 *
 * @return  None.
 */
void network_gatt_add_service(void)
{
    network_profile_svc.p_att_tb = network_profile_att_table;
    network_profile_svc.att_nb = NETWORK_IDX_NB;
    network_profile_svc.gatt_msg_handler = network_gatt_msg_handler;

    network_svc_id = gatt_add_service(&network_profile_svc);
}
